/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.entity.UserAddress;
import com.pig4cloud.pigx.user.service.UserAddressService;
import com.pig4cloud.pigx.user.vo.AppUserAddressVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 
 *
 * @author pigx code generator
 * @date 2022-11-21 11:33:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user_address" )
@Api(value = "user_address", tags = "管理")
public class UserAddressController {

    private final  UserAddressService userAddressService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param userAddress 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('user_useraddress_view')" )
    public R<Page<UserAddress>> getUserAddressPage(Page<UserAddress> page, UserAddress userAddress) {
		page = userAddressService.page(page, Wrappers.query(userAddress));
		page.getRecords().forEach(e -> {
			e.setPrivateKey(null);
			e.setPublicPrivateKey(null);
		});
		return R.ok(page);
    }

	/**
	 * 获取用户充值地址
	 * @return r
	 */
	@GetMapping("/app")
	public R<List<AppUserAddressVO>> getAppUserAddress() {
		return userAddressService.getAppUserAddress();
	}

    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_useraddress_view')" )
    public R getById(@PathVariable("id" ) Long id) {
		UserAddress byId = userAddressService.getById(id);
		byId.setPrivateKey(null);
		byId.setPublicPrivateKey(null);
		return R.ok(byId);
    }

    /**
     * 新增
     * @param userAddress 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_useraddress_add')" )
    public R save(@RequestBody UserAddress userAddress) {
        return R.ok(userAddressService.save(userAddress));
    }

    /**
     * 修改
     * @param userAddress 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_useraddress_edit')" )
    public R updateById(@RequestBody UserAddress userAddress) {
        return R.ok(userAddressService.updateById(userAddress));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_useraddress_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(userAddressService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param userAddress 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_useraddress_export')" )
    public List<UserAddress> export(UserAddress userAddress) {
        return userAddressService.list(Wrappers.query(userAddress));
    }
}
